package algotithm.leetcode.listnode.test1670;

import java.util.LinkedList;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/22-22:30
 * @URL https://leetcode-cn.com/problems/design-front-middle-back-queue/
 * @Title 1670. 设计前中后队列
 */
public class FrontMiddleBackQueue {
    LinkedList<Integer> FBMqueue;
    public FrontMiddleBackQueue() {
        this.FBMqueue=new LinkedList<Integer>();
    }
    public void pushFront(int val) {
        FBMqueue.addFirst(val);
    }

    public void pushMiddle(int val) {
        int index=FBMqueue.size()/2;
        FBMqueue.add(index,val);
    }

    public void pushBack(int val) {
        FBMqueue.addLast(val);
    }

    public int popFront() {
        if(FBMqueue.isEmpty()) {
            return -1;
        }
        return FBMqueue.removeFirst();
    }

    public int popMiddle() {
        int index=FBMqueue.size()/2;
        if(FBMqueue.isEmpty()) {
            return -1;
        }
        if(FBMqueue.size()%2==0) {
            return FBMqueue.remove(index-1);
        }
        return FBMqueue.remove(index);
    }

    public int popBack() {
        if(FBMqueue.isEmpty()) return -1;
        return FBMqueue.removeLast();
    }
}

/**
 * Your FrontMiddleBackQueue object will be instantiated and called as such:
 * FrontMiddleBackQueue obj = new FrontMiddleBackQueue();
 * obj.pushFront(val);
 * obj.pushMiddle(val);
 * obj.pushBack(val);
 * int param_4 = obj.popFront();
 * int param_5 = obj.popMiddle();
 * int param_6 = obj.popBack();
 */


